package com.xiaohong.search.controller;

import com.xiaohong.search.interceptor.TokenRequestInterceptor;
import com.xiaohong.search.pojo.SkuInfo;
import com.xiaohong.search.service.SearchSpuService;
import entity.Result;
import entity.StatusCode;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping(value = "/spu")
public class SearchSpuController {

    @Autowired
    private SearchSpuService searchSpuService;

    /**
     * 重新导入数据
     */
    @GetMapping("/reImport")
    public Result reImportData(HttpServletRequest httpServletRequest) throws IOException {
        //获取请求头里的token,待会feign时候用
        TokenRequestInterceptor.token = httpServletRequest.getHeader("Authorization");
        searchSpuService.reImportData();
        return new Result(true, StatusCode.OK,"重新导入数据成功！");
    }

    /**
     * 数据导入
     */
    @GetMapping("/import")
    public Result importData(HttpServletRequest httpServletRequest) throws IOException {
        //获取请求头里的token,待会feign时候用
        TokenRequestInterceptor.token = httpServletRequest.getHeader("Authorization");
        searchSpuService.importData();
        return new Result(true, StatusCode.OK,"数据添加成功！");
    }

    /**
     * 删除索引
     * @return
     * @throws IOException
     */
    @DeleteMapping("/index/delete")
    public Result deleteIndex() throws IOException {
        searchSpuService.deleteIndex();
        return new Result(true, StatusCode.OK,"删除索引成功！");
    }

    /**
     * 判断索引是否存在
     * @return
     * @throws IOException
     */
    @GetMapping("/index/exists")
    public Result isIndexExists() throws IOException {
        Boolean indexExists = searchSpuService.isIndexExists();
        return new Result(true, StatusCode.OK,"判断索引成功！",indexExists);
    }

    /**
     * 添加文档
     * @param skuInfo
     * @return
     * @throws IOException
     */
    @PostMapping("/document/add")
    public Result addOneDocument(@RequestBody SkuInfo skuInfo) throws IOException {
        IndexResponse indexResponse = searchSpuService.addOneDocument(skuInfo);
        if (indexResponse.getResult().name().equals("CREATED")){
            return new Result(true, StatusCode.OK,"添加文档成功！");
        }else {
            return new Result(false, StatusCode.OK,"添加文档失败！");
        }
    }

    /**
     * 获取文档
     * @param id
     * @return
     * @throws IOException
     */
    @GetMapping("/document/get/{id}")
    public Result getDocument(@PathVariable(value = "id") String id) throws IOException {
        String document = searchSpuService.getDocument(id);
        if (!StringUtils.isEmpty(document)){
            return new Result(true, StatusCode.OK,"获取文档成功！",document);
        }else {
            return new Result(false, StatusCode.OK,"获取文档失败！");
        }
    }

    /**
     * 删除文档
     * @param id
     * @return
     * @throws IOException
     */
    @DeleteMapping("/document/delete/{id}")
    public Result deleteDocument(@PathVariable(value = "id") String id) throws IOException {
        DeleteResponse deleteResponse = searchSpuService.deleteDocument(id);
        if (deleteResponse.getResult().name().equals("DELETED")){
            return new Result(true, StatusCode.OK,"删除文档成功！");
        }else {
            return new Result(false, StatusCode.OK,"删除文档失败！");
        }
    }

    /**
     * 更新文档
     * @param skuInfo
     * @return
     */
    @PutMapping("/document/update")
    public Result updateDocument(@RequestBody SkuInfo skuInfo) {
        UpdateResponse updateResponse = null;
        try {
            updateResponse = searchSpuService.updateDocument(skuInfo.getId() + "", skuInfo);
            if (updateResponse.getResult().name().equals("UPDATED")){
                return new Result(true, StatusCode.OK,"更新文档成功！");
            }else {
                return new Result(false, StatusCode.OK,"更新文档失败！");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, StatusCode.OK,"更新文档失败！");
        }
    }

    /**
     * 搜索
     * @param searchMap
     * @return
     */
    @GetMapping("/search")
    public Map search(@RequestParam(required = false) Map<String,String> searchMap) throws IOException {
        return  searchSpuService.search(searchMap);
    }
}
